import vtkmodules.all as vtk
import copy


class SphereActor:
    type = "GRAPHIC_ACTOR"
    created_by = "default"

    def __init__(self, center, options={}):
        self.mapper = vtk.vtkPolyDataMapper()
        self.source = vtk.vtkSphereSource()
        self.center = center
        self.options = options
        self.instance = vtk.vtkActor()
        self.source.SetCenter(center)
        self.source.SetRadius(self.options.get('radius', 1.0))
        self.source.SetPhiResolution(100)
        self.source.SetThetaResolution(100)
        self.source.Update()

        self.mapper.SetInputConnection(self.source.GetOutputPort())
        self.instance.SetMapper(self.mapper);

        if options.get("color", None) is not None:
            r, g, b = options.get("color") or [0, 0, 1]
            self.instance.GetProperty().SetColor((r, g, b))

    def setPosition(self, center):
        center = copy.copy(center)
        self.center = center
        self.instance.SetPosition(center)

    def getPosition(self):
        return self.instance.GetPosition()

    def getCenter(self):
        return self.center

    def setRadius(self, radius):
        self.source.SetRadius(radius)
        self.source.Update()

    def getRadius(self):
        return self.source.GetRadius()

    def clone(self):
        config = {}
        if self.options.get("color", None) is not None:
            config["color"] = self.options.get("color")
        if self.options.get("radius", None) is not None:
            config["radius"] = self.options.get("radius")
        return SphereActor(self.center, config)

    def setVisible(self, isVisible):
        self.instance.SetVisibility(isVisible)

    def getVisible(self):
        return self.instance.GetVisibility()
